<!--
 * @Description: 商家端优惠券列表
-->
<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="优惠券名称">
        <el-input v-model="formInline.couponName" placeholder></el-input>
      </el-form-item>
      <el-form-item label="是否上架">
        <el-select v-model="formInline.isJoin">
          <el-option label="全部" value></el-option>
          <el-option label="上架" :value="1"></el-option>
          <el-option label="下架" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="search()" size="mini">查询</el-button>
        <el-button type="success" plain @click="kong()" size="mini" icon="el-icon-delete">清空</el-button>
        <!-- <el-button type="warning" plain icon="el-icon-document-checked" @click="allAudit()" size="mini">批量审核</el-button> -->
      </el-form-item>
    </el-form>

    <!-- @selection-change="handleSelectionChange" -->
    <el-table :data="tableData" style="width: 100%" v-loading="loading" border
      :header-cell-style="{ background: '#ebecf0', color: '#606266' }" height="600px">
      <!-- <el-table-column type="selection" width="55" header-align="center" align="center" :selectable="selectable" /> -->
      <el-table-column prop="couponSeq" label="优惠券编号" header-align="center" width="150" align="center"></el-table-column>
      <el-table-column prop="couponName" label="优惠券名称" header-align="center" align="center"></el-table-column>
      <el-table-column label="有效期" header-align="center" width="220" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.validType == '1' || scope.row.validType == '3'">{{ scope.row.validDate }}{{ scope.row.validType == '1' ? '天' : '个月' }}</span>
          <span v-if="scope.row.validType == '2'">{{ scope.row.validCreateDate }}至{{ scope.row.validEndDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="优惠类型" header-align="center" align="center">
        <template slot-scope="scope">
          {{ scope.row.discountType == '1' ? '折扣' : '满减' }}
        </template>
      </el-table-column>
      <el-table-column prop="couponMoney" label="优惠券金额" header-align="center" align="center">
        <template slot-scope="scope">
          <!-- <span v-if="scope.row.discountType == 0">满{{ scope.row.conponLimitMoney }}减{{
            scope.row.couponMoney
          }}元</span> -->
          <span v-if="scope.row.discountType == 0">{{
            scope.row.couponMoney
          }}元</span>
          <span v-else>{{ scope.row.couponDiscount }}折</span>
        </template>
      </el-table-column>
      <el-table-column label="已领取数量" header-align="center" prop="receiveNum" align="center"></el-table-column>
      <el-table-column label="参与门店" header-align="center" prop="storeName" align="center"></el-table-column>
      <el-table-column label="更新时间" header-align="center" prop="updateTime" align="center"></el-table-column>
      <el-table-column label="是否上架" header-align="center" width="100px" aligin="center" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isJoin" active-color="#13ce66" :active-value="1" :inactive-value="0"
            @change="updateStatus(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" header-align="center">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == 1" type="text" size="mini"  @click="sendHandle(scope.row)">分发</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 50, 100, 200]"
        :current-page="listQuery.current"
        :page-size="listQuery.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import {
  queryCouponStorePageList,
  couponJoinByStore,
} from '@/api/promotionProgramEdit';
export default {
  data() {
    return {
      formInline: {
        couponName: '',
        isJoin: '',
        orgSeq: '',
      },
      tableData: [],
      loading: false,
      total: 0,
      listQuery: {
        current: 1,
        size: 20,
      },
      multipleSelection: []
    }
  },
  created() {
    const logininfo = localStorage.getItem('logininfo');
    const orgSeq = JSON.parse(logininfo).orgSeq || JSON.parse(logininfo).orgList[0].orgSeq;
    this.formInline.orgSeq = orgSeq;
    this.couponList();
  },
  methods: {
    search() {
      this.listQuery.current = 1;
      this.couponList();
    },
    handleSizeChange(val) {
      this.listQuery.size = val;
      this.couponList();
    },
    handleCurrentChange(val) {
      this.listQuery.current = val;
      this.couponList();
    },
    kong() {
      this.formInline.couponName = '';
      this.formInline.isJoin = '';
      this.search();
    },

    couponList() {
      const data = {
        size: this.listQuery.size,
        current: this.listQuery.current,
        condition: {
          couponType: 2,
          status: 1,
          couponName: this.formInline.couponName,
          orgSeq: this.formInline.orgSeq,
          isJoin: this.formInline.isJoin,
          channel: 'web'
        },
        ascs: [],
        descs: [],
      };

      this.loading = true;
      queryCouponStorePageList(data).then((res) => {
        this.tableData = res.records;
        this.total = res.total;
      }).finally(() => {
        this.loading = false
      });
    },

    updateStatus(row) {
      row.isJoin = row.isJoin == 1 ? 0 : 1;
      this.$confirm(`是否${row.isJoin == 1 ? '下架' : '上架'}状态?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        couponJoinByStore({
          orgSeq: this.formInline.orgSeq,
          couponIds: [row.uuid],
          isJoin: row.isJoin == 1 ? 0 : 1,
        }).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: '操作成功',
              type: 'success',
            });
            this.search();
          } else {
            this.$message.error(res.message || '操作失败');
          }
        })
      })
    }
  },

  selectable(row) {
    return row.isJoin == 0
  },

  allAudit() {

  },

  handleSelectionChange(val) {
    val.length > 0 && (this.multipleSelection = val.map(item => item.uuid));
  },
}
</script>

<style lang="scss" scoped></style>